﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Newtonsoft.Json;
using Enter.Common;
using Enter.Common.Helper;

namespace Enter.DataProxy.Cache.Configuration
{
    internal class CacheCommon
    {
        /// <summary>
        /// 类序列化成字符
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ObjToString(object obj)
        {
            string result = string.Empty;

            if (obj != null)
            {
                Type ty = obj.GetType();
                Type ty1 = ty.BaseType;
                if (ty1.Name == "ValueType")
                {
                    result = JsonConvert.SerializeObject(obj);
                }
                else
                {
                    if ((ty.Name == "String") || (ty.Name == "StringBuilder"))
                    {
                        result = obj.ToString();
                    }
                    else
                    {
                        if (obj.GetType().IsSerializable)
                        {
                            try
                            {
                                MemoryStream outputStream = new MemoryStream();
                                new BinaryFormatter().Serialize(outputStream, obj);
                                outputStream.Position = 0L;
                                Byte[] byte1 = outputStream.GetBuffer();
                                result = Convert.ToBase64String(byte1);
                            }
                            catch (Exception e)
                            {
                                Newtonsoft.Json.JsonSerializerSettings js = new JsonSerializerSettings();
                                js.TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full;
                                js.TypeNameHandling = TypeNameHandling.All;
                                result = Newtonsoft.Json.JsonConvert.SerializeObject(obj, Formatting.Indented, js);
                            }
                        }
                        else
                        {
                            Newtonsoft.Json.JsonSerializerSettings js = new JsonSerializerSettings();
                            js.TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full;
                            js.TypeNameHandling = TypeNameHandling.All;

                            result = Newtonsoft.Json.JsonConvert.SerializeObject(obj, Formatting.Indented, js);
                        }
                    }

                }
            }

            return result;

        }

        /// <summary>
        /// 字符转化成类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ty"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T StringToObj<T>(string value)
        {
            object result = new object();
            try
            {
                Type ty = typeof(T);
                Type ty1 = ty.BaseType;
                if ((ty1 != null) && (ty1.Name == "ValueType"))
                {
                    result = JsonConvert.DeserializeObject(value, ty);
                }
                else
                {
                    if ((ty.Name == "String") || (ty.Name == "StringBuilder"))
                    {
                        if (ty.Name == "String")
                        {
                            result = value.ToString();
                        }
                        else
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.Append(value);
                            result = sb;
                        }
                    }
                    else
                    {
                        if ((value.Substring(0, 1) == "{") || (value.Substring(0, 2) == "[{"))
                        {
                            Newtonsoft.Json.JsonSerializerSettings js = new JsonSerializerSettings();
                            js.TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full;
                            js.TypeNameHandling = TypeNameHandling.All;
                            result = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(value, js);

                        }
                        else
                        {
                            MemoryStream memStream2 = new MemoryStream(Convert.FromBase64String(value));
                            BinaryFormatter formatter = new BinaryFormatter();
                            result = formatter.Deserialize(memStream2);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogHelper.Log(e);
                result = null;
            }
            if (result != null)
            {
                return (T)result;
            }
            return default(T);
        }

    }
}
